//
//  XiaoximenVoteBasicInfoViewController.m
//  Xiaoximen_iOS
//
//  Created by Wang Zhongyu on 12-7-17.
//  Copyright (c) 2012年 Peking University. All rights reserved.
//

#import "XiaoximenVoteBasicInfoViewController.h"
#import "XiaoximenVotePartyInfoViewController.h"
#import "XiaoximenAppDelegate.h"

@interface XiaoximenVoteBasicInfoViewController () {
	int detailPartyID;
}

@end

@implementation XiaoximenVoteBasicInfoViewController
@synthesize parties;
@synthesize images = _images;
@synthesize partiesTableView;

- (NSMutableArray *)images
{
	if (_images == nil) _images = [[NSMutableArray alloc] init];
	while (_images.count < parties.count) {
		UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 43)];
		imageView.image = [UIImage imageNamed:@"text"];
		[_images addObject:imageView];
	}
	return _images;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bar"] forBarMetrics:UIBarMetricsDefault];
}
- (void)viewDidUnload
{
	[self setPartiesTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return parties.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"partyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
	[cell insertSubview:[self.images objectAtIndex:indexPath.row] atIndex:0];
	
	
	UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 2, 180, 21)];
	label.text = [[parties objectAtIndex:indexPath.row] title];
	label.font = [UIFont boldSystemFontOfSize:17];
	label.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
	[cell addSubview:label];
	
	UILabel *personLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 180, 21)];
	personLabel.text = [[parties objectAtIndex:indexPath.row] host];
	personLabel.font = [UIFont systemFontOfSize:14];
	personLabel.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
	[cell addSubview:personLabel];
	
    return cell;
}

- (void)reloadParties:(NSMutableArray *)_parties
{
	self.parties = _parties;
	[self.partiesTableView reloadData];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
	XiaoximenVotePartyInfoViewController *vpivc  = segue.destinationViewController;
	vpivc.party = [parties objectAtIndex:detailPartyID];
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	detailPartyID = indexPath.row;
	[self performSegueWithIdentifier:@"EnterPartySegue" sender:self];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (IBAction)onBack:(id)sender {
	[self dismissModalViewControllerAnimated:YES];
}
@end
